package org.keyyond.ms.business.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.keyyond.ms.business.model.MsBusinessMessageLog;
import org.keyyond.ms.business.util.CommonBeanutils;
import org.keyyond.ms.business.util.Constant;
import org.keyyond.ms.business.util.JedisPoolUtils;
import org.keyyond.ms.core.util.ConstantResult;
import org.keyyond.ms.core.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;

import redis.clients.jedis.Jedis;



/**
 * @author songjianping
 * @belong to Shccs
 * @date:2018-6-12(V1.0)
 * @date:2018-6-12(V1.0)
 * @version : V1.0
 */

@RequestMapping("redis")
@RestController
public class MsBusinessRedisController extends CommonBeanutils{
	
	@Autowired
	private JedisPoolUtils jedisPoolUtils;

	
	/**
	 * Redis存储服务
	 * 
	 * @author songjianping
	 * @since 2018-6-12
	 * @category </br>Redis存储服务</br>
	 *           
	 * @param jsonStr
	 *            | string | 必填 {key: ,value: ,time: }
	 * @return <b>[成功返回]：</b><b>{result:true}</br> <b>[失败返回]:</b><b>{result:false,errmsg:}</br>
	 * @exception 
	 */
	@PostMapping("/addPropertiesToRedis")
	public String addPropertiesToRedis(HttpServletRequest request,HttpServletResponse response){
		String traceId = request.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
		MsBusinessMessageLog messageLog = new MsBusinessMessageLog();
		messageLog.setTraceid(traceId);
		messageLog.setLogtype(Constant.CONST_MESSAGE_LOG_EXCEPTION);
		String result = new String();
		JSONObject resultData = new JSONObject();
		String param = request.getParameter(Constant.CONST_ZBGS_PARAM);
		messageLog.setContent(param);
		try {
		 Map<String, String> map = JsonUtil.JsonToMap(param);
		StringBuffer msg = new StringBuffer();
		if(map.get("key")==null||map.get("key").equals("")){
			msg.append("key:不能为空,");
		}
		if(map.get("value")==null||map.get("value").equals("")){
			msg.append("value:不能为空,");
		}
		if(map.get("time")!=null&&map.get("time").equals("")){
			msg.append("time:时间不能为空,");
		}
		if(!msg.toString().equals("")){
			resultData.put(ConstantResult.RESULT_ERRMSG, msg.toString());
			resultData.put(ConstantResult.RESULT_ERRCODE, ConstantResult.RESULT_ERRCODE_PARAM);
			result = returnResult(false, resultData);
			messageLog.setReturnmessage(msg.toString());
		}else{
			String time = map.get("time");
			String setResult;
			if(time==null){
				setResult = jedisPoolUtils.setString(String.valueOf(map.get("key")),String.valueOf(map.get("value")));
			}else{
				setResult=jedisPoolUtils.setString(String.valueOf(map.get("key")),Integer.parseInt(time),String.valueOf(map.get("value")));
			}
			if(setResult.equals("OK")){
				resultData.put(ConstantResult.RESULT_MESSAGE, ConstantResult.RESULT_SUCCESS_ADD);
				result = returnResult(true, resultData);
				messageLog.setReturnmessage(setResult);
			}else{
				resultData.put(ConstantResult.RESULT_ERRMSG, ConstantResult.RESULT_ERRMSG_ADD);
				resultData.put(ConstantResult.RESULT_ERRCODE, ConstantResult.RESULT_ERRCODE_ADD);
				result = returnResult(false, resultData);
				messageLog.setReturnmessage(ConstantResult.RESULT_ERRMSG_ADD);
			}
			 messageLog.setLogtype(Constant.CONST_MEESAGE_LOG_SERVICE);
		}
		}catch(Exception e) {
		    e.printStackTrace();
		    resultData.put(ConstantResult.RESULT_ERRMSG, ConstantResult.RESULT_ERRMSG_EXCEPTION);
		    resultData.put(ConstantResult.RESULT_ERRCODE, ConstantResult.RESULT_ERRCODE_EXCEPTION);
		    result = returnResult(false, resultData);
		    messageLog.setReturnmessage(ConstantResult.RESULT_ERRMSG_EXCEPTION);
		}
		messageLog.setReturnmessage(result);
		addMessageLog(messageLog);
		return result;
	}
	
	/**
	 * Redis读取服务
	 * 
	 * @author songjianping
	 * @since 2018-6-12
	 * @category </br>Redis读取服务</br>
	 *           
	 * @param jsonStr
	 *            | string | 必填 {key: }
	 * @return <b>[成功返回]：</b><b>{result:true,data{message:}}</br> <b>[失败返回]:</b><b>{result:false,errmsg:}</br>
	 * @exception 
	 */
	@PostMapping("/getValueFromRedisByKey")
	public String getValueFromRedisByKey(HttpServletRequest request,HttpServletResponse response){
		String traceId = request.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
		MsBusinessMessageLog messageLog = new MsBusinessMessageLog();
		messageLog.setTraceid(traceId);
		messageLog.setLogtype(Constant.CONST_MESSAGE_LOG_EXCEPTION);
		String result = new String();
		JSONObject resultData = new JSONObject();
		String param = request.getParameter(Constant.CONST_ZBGS_PARAM);
		messageLog.setContent(param);
		try {
		 Map<String, String> map = JsonUtil.JsonToMap(param);
		StringBuffer msg = new StringBuffer();
		if(map.get("key")==null||map.get("key").equals("")){
			msg.append("key:不能为空,");
		}
		if(!msg.toString().equals("")){
			resultData.put(ConstantResult.RESULT_ERRMSG, msg.toString());
			resultData.put(ConstantResult.RESULT_ERRCODE, ConstantResult.RESULT_ERRCODE_PARAM);
			result = returnResult(false, resultData);
			messageLog.setReturnmessage(msg.toString());
		}else{
			 String value = jedisPoolUtils.getString(String.valueOf(map.get("key")));
			 if(value!=null){
				 resultData.put(ConstantResult.RESULT_MESSAGE,value);
				 result = returnResult(true, resultData);
				 messageLog.setReturnmessage(value);
			 }else{
				 	resultData.put(ConstantResult.RESULT_ERRMSG, "对应的value不存在.");
					resultData.put(ConstantResult.RESULT_ERRCODE, ConstantResult.RESULT_ERRCODE_ACTION);
					result = returnResult(false, resultData);
					messageLog.setReturnmessage("对应的value不存在.");
			 }
			 messageLog.setLogtype(Constant.CONST_MEESAGE_LOG_SERVICE);
		}
		}catch(Exception e) {
		    e.printStackTrace();
		    resultData.put(ConstantResult.RESULT_ERRMSG, ConstantResult.RESULT_ERRMSG_EXCEPTION);
		    resultData.put(ConstantResult.RESULT_ERRCODE, ConstantResult.RESULT_ERRCODE_EXCEPTION);
		    result = returnResult(false, resultData);
		    messageLog.setReturnmessage(ConstantResult.RESULT_ERRMSG_EXCEPTION);
		}
		messageLog.setReturnmessage(result);
		addMessageLog(messageLog);
		return result;
	}
	
	/**
	 * Redis删除服务
	 * 
	 * @author songjianping
	 * @since 2018-6-12
	 * @category </br>Redis删除服务</br>
	 *           
	 * @param jsonStr
	 *            | string | 必填 {key: }
	 * @return <b>[成功返回]：</b><b>{result:true}}</br> <b>[失败返回]:</b><b>{result:false,errmsg:}</br>
	 * @exception 
	 */
	@PostMapping("/delValueFromRedisByKey")
	public String delValueFromRedisByKey(HttpServletRequest request,HttpServletResponse response){
		String traceId = request.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
		MsBusinessMessageLog messageLog = new MsBusinessMessageLog();
		messageLog.setTraceid(traceId);
		messageLog.setLogtype(Constant.CONST_MESSAGE_LOG_EXCEPTION);
		String result = new String();
		JSONObject resultData = new JSONObject();
		String param = request.getParameter(Constant.CONST_ZBGS_PARAM);
		messageLog.setContent(param);
		try {
		 Map<String, String> map = JsonUtil.JsonToMap(param);
		StringBuffer msg = new StringBuffer();
		if(map.get("key")==null||map.get("key").equals("")){
			msg.append("key:不能为空,");
		}
		if(!msg.toString().equals("")){
			resultData.put(ConstantResult.RESULT_ERRMSG, msg.toString());
			resultData.put(ConstantResult.RESULT_ERRCODE, ConstantResult.RESULT_ERRCODE_PARAM);
			result = returnResult(false, resultData);
			messageLog.setReturnmessage(msg.toString());
		}else{
			 Long del = jedisPoolUtils.delString(String.valueOf(map.get("key")));
			 if(del>0){
				 resultData.put(ConstantResult.RESULT_MESSAGE,ConstantResult.RESULT_SUCCESS_ACTION);
				 result = returnResult(true, resultData);
				 messageLog.setReturnmessage(ConstantResult.RESULT_SUCCESS_ACTION);
			 }else{
				 	resultData.put(ConstantResult.RESULT_ERRMSG, ConstantResult.RESULT_ERRMSG_ACTION);
					resultData.put(ConstantResult.RESULT_ERRCODE, ConstantResult.RESULT_ERRCODE_ACTION);
					result = returnResult(false, resultData);
					messageLog.setReturnmessage(ConstantResult.RESULT_ERRMSG_ACTION);
			 }
			 messageLog.setLogtype(Constant.CONST_MEESAGE_LOG_SERVICE);
		}
		}catch(Exception e) {
		    e.printStackTrace();
		    resultData.put(ConstantResult.RESULT_ERRMSG, ConstantResult.RESULT_ERRMSG_EXCEPTION);
		    resultData.put(ConstantResult.RESULT_ERRCODE, ConstantResult.RESULT_ERRCODE_EXCEPTION);
		    result = returnResult(false, resultData);
		    messageLog.setReturnmessage(ConstantResult.RESULT_ERRMSG_EXCEPTION);
		}
		messageLog.setReturnmessage(result);
		addMessageLog(messageLog);
		return result;
	}
	
	/**
	 * Redis修改服务
	 * 
	 * @author songjianping
	 * @since 2018-6-12
	 * @category </br>Redis修改服务</br>
	 *           
	 * @param jsonStr
	 *            | string | 必填 {key: ,value: ,time: }
	 * @return <b>[成功返回]：</b><b>{result:true}}</br> <b>[失败返回]:</b><b>{result:false,errmsg:}</br>
	 * @exception 
	 */
	@PostMapping("/updateValueFromRedisByKey")
	public String updateValueFromRedisByKey(HttpServletRequest request,HttpServletResponse response){
		String traceId = request.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
		MsBusinessMessageLog messageLog = new MsBusinessMessageLog();
		messageLog.setTraceid(traceId);
		messageLog.setLogtype(Constant.CONST_MESSAGE_LOG_EXCEPTION);
		String result = new String();
		JSONObject resultData = new JSONObject();
		String param = request.getParameter(Constant.CONST_ZBGS_PARAM);
		messageLog.setContent(param);
		try {
		 Map<String, String> map = JsonUtil.JsonToMap(param);
		StringBuffer msg = new StringBuffer();
		if(map.get("key")==null||map.get("key").equals("")){
			msg.append("key:不能为空,");
		}
		if(map.get("value")==null||map.get("value").equals("")){
			msg.append("value:不能为空,");
		}
		if(map.get("time")!=null&&map.get("time").equals("")){
			msg.append("time:时间不能为空,");
		}
		if(!msg.toString().equals("")){
			resultData.put(ConstantResult.RESULT_ERRMSG, msg.toString());
			resultData.put(ConstantResult.RESULT_ERRCODE, ConstantResult.RESULT_ERRCODE_PARAM);
			result = returnResult(false, resultData);
			messageLog.setReturnmessage(msg.toString());
		}else{
			String time = map.get("time");
			String setResult;
			if(time==null){
				setResult = jedisPoolUtils.setString(String.valueOf(map.get("key")),String.valueOf(map.get("value")));
			}else{
				setResult=jedisPoolUtils.setString(String.valueOf(map.get("key")),Integer.parseInt(time),String.valueOf(map.get("value")));
			}
			if(setResult.equals("OK")){
				resultData.put(ConstantResult.RESULT_MESSAGE, ConstantResult.RESULT_SUCCESS_EDIT);
				result = returnResult(true, resultData);
				messageLog.setReturnmessage(setResult);
			}else{
				resultData.put(ConstantResult.RESULT_ERRMSG, ConstantResult.RESULT_ERRMSG_EDIT);
				resultData.put(ConstantResult.RESULT_ERRCODE, ConstantResult.RESULT_ERRCODE_EDIT);
				result = returnResult(false, resultData);
				messageLog.setReturnmessage(ConstantResult.RESULT_ERRMSG_EDIT);
			}
			 messageLog.setLogtype(Constant.CONST_MEESAGE_LOG_SERVICE);
		}
		}catch(Exception e) {
		    e.printStackTrace();
		    resultData.put(ConstantResult.RESULT_ERRMSG, ConstantResult.RESULT_ERRMSG_EXCEPTION);
		    resultData.put(ConstantResult.RESULT_ERRCODE, ConstantResult.RESULT_ERRCODE_EXCEPTION);
		    result = returnResult(false, resultData);
		    messageLog.setReturnmessage(ConstantResult.RESULT_ERRMSG_EXCEPTION);
		}
		messageLog.setReturnmessage(result);
		addMessageLog(messageLog);
		return result;
	}
	
}
